﻿using Ctrip.Automation.Framework.Attribute;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using Ctrip.Automation.Framework.Lib;
using NUnit.Framework;
using System.IO;
using API.Taolx.Shopping.Common;
using Newtonsoft.Json.Linq;
using System.Diagnostics;
using System.Xml.Linq;
using Gallio.Framework;
using API.Taolx.Shopping.Business;
using System.Configuration;
using System.Xml;

namespace API.Taolx.Shopping.TestCases.Api_Product
{
    [TestFixture]
    public class SearchV4
    {
        string str = string.Empty;
        string urlconfig = string.Empty;
        #region 公共方法
        [SetUp]
        public void SetUp()
        {
             Hashtable htEnv = Xml.GetSettings();
            // responseString = WebSvcCaller.RequestByPostWithXForm(htEnv["OpenAPI"].ToString());
            // Hashtable myNameValue = (Hashtable)System.Configuration.ConfigurationSettings.AppSettings.Get(@"myConfig\myInfo");
           //  string Area = myNameValue["Area"];
           //  string file = Application.ExecutablePath;
           //  System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(file);
            // urlconfig = config.AppSettings["TaolxSOAService"]+ "/api/Product/CalculateMinPrice";
           //  str = ConfigurationManager.ConnectionStrings["SearchV4"].ToString();
            string indexConfigPath = @"E:/Taolx.Test/OpenAPI.Taolx.Shopping/Conf/Config.xml"; 
             
           //ExeConfigurationFileMap ecf = new ExeConfigurationFileMap();
          // ecf.ExeConfigFilename = indexConfigPath;
          // Configuration config = ConfigurationManager.OpenMappedExeConfiguration(ecf, ConfigurationUserLevel.None);
          //  urlconfig = config.AppSettings.Settings["TaolxSOAService"].Value + "/api/Product/CalculateMinPrice";
            //  XmlDataDocument xmlDoc = new System.Xml.XmlDataDocument();
            //   xmlDoc.Load(@"E:/Taolx.Test/OpenAPI.Taolx.Shopping/Conf/Config.xml");
            //    XmlElement elem = xmlDoc.GetElementById("TaolxSOAService");
            //    string str = elem.Attributes["value"].Value;
            

        }



        [TearDown]
        public void TearDown()
        {
            //

        }
        #endregion

        [Test]
        [Description(" 产品查询接口，P1 case,验证IsSuccess as True")]
        [Priority(Priority.P1)]
        public void SearchV4VerifyResultCode()
        {
            string requestStr = String.Empty;
            string Url = "http://shoppingapi.test3.taolx.com/api/Product/SearchV4";
            string str = File.ReadAllText(string.Format("./Request_json/Api-Product/SearchV4.json"));
          
            PrepareRequest request = new PrepareRequest();
            requestStr = request.postAPIFunc(urlconfig,str);

            MoreCheck(requestStr);
            //验证返回报文
           // VerifyResponse verify = new VerifyResponse(requestStr);
           // verify.VerifyResultDataByAttributeValue("true", "IsSuccess");
        }

      

        [Test]
        private void MoreCheck(string requestStr)
        {
            try
            {
                //检查点
                JObject jo = JObject.Parse(requestStr);
                string[] values = jo.Properties().Select(item => item.Value.ToString()).ToArray();
                string IsSuccess = values[1].ToString();
                string ErrorMessage = values[3].ToString();
                // var IsSuccess = jo.Properties().First(item => item.Name.ToString() == "IsSuccess").Value;
                //var ErrorMessage = jo.Properties().First(item => item.Name.ToString() == "ErrorMessage");
                CtripAssert.AreEqual("True", IsSuccess);
                CtripAssert.AreEqual("", ErrorMessage);
                Trace.WriteLine(IsSuccess);
                Trace.WriteLine(ErrorMessage);
                Log.Info("返回IsSuccess:" + IsSuccess);
                Log.Info("返回ErrorMessage:" + ErrorMessage);
            }
            catch (Exception ex)
            {
                //检查是否因Service地址变更或停止工作造成异常
                CtripAssert.Contains(ex.Message, "Not Found");
            }

        }
       

    }
}
